<template>
	<view class="page">

		<view class="group_3">
			<text lines="1" class="text_8">教室图片</text>
			<view class="item">
				<view class="d-flex flex-column align-center justify-center">
					<view class="">
						<u-upload :fileList="fileList1" name="1" width="340px" height="150px" @afterRead="afterRead"
							@delete="deletePic" multiple :maxCount="1">
						</u-upload>
					</view>
				</view>
			</view>

			<text lines="1" class="text_8">所属校区</text>
			<view class="item">
				<input type="text" placeholder="请选择所属校区" disabled v-model="userInfo.occupation_relation"
					@click="showoccupation = true" class="atinput" />
			</view>
			<u-picker :show="showoccupation" :columns="occupation_status" keyName="venue_name" @confirm="addoccupation"
				@cancel="showoccupation = false">
			</u-picker>

			<text lines="1" class="text_8">教室名称</text>
			<view class="item">
				<input type="text" placeholder="请输入教室名称" class="atinput" v-model="name" />
			</view>

			<text lines="1" class="text_8">培训项目</text>
			<view class="item">
				<view class="flex-space-between week-list flex-wrap">
					<view class="week-item" :class="{'active':item.id === selectId}" v-for="(item,index) in weekList"
						:key="index" @click="timesSelect(item,index)">
						{{item.name}}
					</view>
					<!-- <view class="week-item" :class="{'active': weekSelect.includes(item.id)}" v-for="item in weekList"
						:key="item.id" @click="handSelect(item)">
						{{ item.name }}
					</view> -->
					<!-- <view class="check">
						<van-checkbox :value="isAllSelect" @change="handAllSelect" label-position="left" icon-size="32rpx"
							shape="square" checked-color="#655ff7">
							全选
						</van-checkbox>
					</view> -->
				</view>
			</view>
			<text lines="1" class="text_8">容纳人数</text>
			<view class="item">
				<input type="number" placeholder="请输入容纳人数" class="atinput" v-model="students" />
			</view>
			<text lines="1" class="text_8">面积</text>
			<view class="item">
				<input type="text" placeholder="请输入教室面积" class="atinput" v-model="area" />
			</view>
			<text lines="1" class="text_8">所属楼层</text>
			<view class="item">
				<input type="text" placeholder="请输入所属楼层" class="atinput" v-model="floor" />
			</view>
			<text lines="1" class="text_8">备注说明</text>
			<view class="item">
				<u-textarea v-model="remark" placeholder="请输入备注说明" maxlength='-1'></u-textarea>
			</view>
			<view class="" style="width: 100%;height: 12vh;"></view>
		</view>
		<view class="d-flex flex-row justify-space-between justify-center align-center"
			style="position: fixed;bottom: 0;height: 120rpx;background-color: #fff;width: 100%;padding: 0 6%;z-index: 99;">
			<view class="" @click="addclick"
				style="width: 100%;height: 80rpx;background: linear-gradient(0deg, rgba(214, 53, 53, 0.9), rgba(255, 64, 64, 0.93), rgba(250, 142, 142, 0.93));color: #ffffff;font-size: 24rpx;text-align: center;line-height: 80rpx;border-radius: 35rpx;">
				确定创建
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	const venues = require("@/api/venues/venues.js");
	export default {
		data() {
			return {
				fileList1: [],
				url_phone: '',
				fileList: [], //图片列表
				showoccupation: false, //职业
				occupation_status: [], //职业
				occupation_list: [],

				userInfo: {
					occupation_relation: '',
					occupation_relation_id: '',
					occupation_jineng: '',
					occupation_jineng_id: '',
					occupation_changguan: "",
					occupation_changguan_id: "",
					occupation_xcx: '',
					occupation_xcx_id: '',
				},
				weekSelect: [], //选中的
				weekList: [{
						id: '1',
						name: '篮球训练'
					},
					{
						id: '2',
						name: '游泳课程'
					},
					{
						id: '3',
						name: '瑜伽课程'
					},
					{
						id: '4',
						name: '健生教练'
					},
					{
						id: '5',
						name: '羽毛球培训'
					}
				],
				pid: '',
				remark: '',
				selectId: 0,
				name: '',
				students: '',
				project: '',
				area: '',
				floor: '',
				remark: '',
				id: ''
			}
		},
		onLoad(options) {
			this.pid = options.pid
			if (options.status == 1) {
				const selectionArrs = JSON.parse(decodeURIComponent(options.selectionArr))
				this.schoollist = selectionArrs
				console.log(this.schoollist, 'this.schoollist')
				this.fileList1.push({
					url: this.schoollist.pic
				});
				this.url_phone = this.schoollist.pic
				this.userInfo.occupation_relation= this.schoollist.venue_name
				this.userInfo.occupation_relation_id = this.schoollist.area_id
				this.name = this.schoollist.classroom_name
				this.area = this.schoollist.classroom_area
				this.students = this.schoollist.classroom_students
				this.floor = this.schoollist.floor
				this.remark = this.schoollist.remark
				// this.projectIds = this.schoollist.train_program.map(project => project.id);
				// this.jiaoSelect = [...this.projectIds];
				// const initialSelectedIds = this.jiaoSelect;
				// initialSelectedIds.forEach(id => {
				// 	if (!this.jiaoSelect.includes(id)) {
				// 		this.weekList.push(id);
				// 	}
				// });
				this.id = this.schoollist.id
				this.selectId = this.schoollist.train_program[0].id
				this.getProjects11()
			}else{
				this.getProjects()
			}
			this.getSchoolList()
		},
		methods: {
			timesSelect(item, index) {
				this.selectId = item.id
			},
			async addclick() {
				let that = this
				if (that.url_phone == '') {
					uni.showToast({
						title: '请输入教室图片',
						icon: 'none'
					})
					return
				}
				if (that.userInfo.occupation_relation_id == '') {
					uni.showToast({
						title: '请选择所属校区',
						icon: 'none'
					})
					return
				}
				if (that.name == '') {
					uni.showToast({
						title: '请输入教室名称',
						icon: 'none'
					})
					return
				}
				if (this.selectId == '') {
					uni.showToast({
						title: '请输入培训项目',
						icon: 'none'
					})
					return
				}
				if (this.students == '') {
					uni.showToast({
						title: '请输入容纳人数',
						icon: 'none'
					})
					return
				}
				if (this.area == '') {
					uni.showToast({
						title: '请输入面积',
						icon: 'none'
					})
					return
				}
				if (this.floor == '' || this.floor == null) {
					uni.showToast({
						title: '请输入所属楼层',
						icon: 'none'
					})
					return
				}
				let params = {
					pic: this.url_phone,
					area_id: this.userInfo.occupation_relation_id,
					name: this.name,
					project: this.selectId,
					students: this.students,
					area: this.area,
					floor: this.floor,
					remark: this.remark,
					pid: this.pid,
					id: this.id
				};
				let res = await venues.createClassRoom(params);
				if (res.code === 1) {
					uni.$u.toast(res.info);
					setTimeout(() => {
						uni.navigateBack({
							delta: 1 // 返回上一页面，如果页面栈中只有一个页面则该方法不会生效
						});
					}, 1000)
				}
			},
			async getSchoolList() {
				let params = {
					pid: this.pid
				};
				let res = await venues.getSchoolList(params);
				if (res.code === 1) {
					this.occupation_status.push(
						res.data
					)
				}
			},
			async getProjects() {
				let params = {
					pid: this.pid,
				};
				let res = await venues.getProjects(params);
				if (res.code === 1) {
					this.weekList = res.data
					this.selectId = res.data[0].id
				}
			},
			async getProjects11() {
				let params = {
					pid: this.pid,
				};
				let res = await venues.getProjects(params);
				if (res.code === 1) {
					this.weekList = res.data
				}
			},
			valChange(e) {},
			handSelect(item) {
				let data = item.id;
				// 使用indexOf检查是否已选中，避免重复或遗漏
				const index = this.weekSelect.indexOf(data);
				if (index > -1) {
					// 如果已选中，则移除
					this.weekSelect.splice(index, 1);
				} else {
					// 如果未选中，则添加
					this.weekSelect.push(data);
				}
			},
			// 职业
			addoccupation(e) {
				this.userInfo.occupation_relation = e.value[0].venue_name
				this.userInfo.occupation_relation_id = e.value[0].id
				this.showoccupation = false
			},

			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: this.url_phone
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://sass-test.doit10019.com/api/upload/uploadOneImage', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							// user: 'test',
							project_id: getApp().globalData.projectId,
							app_id: this.$https.weixinAppId,

						},
						success: (res) => {
							let result = JSON.parse(res.data);
							this.url_phone = result.data.url
							setTimeout(() => {
								resolve(this.url_phone)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		width: 100%;
		// height: 100vh;
		background-color: #EDF5F5;

		.group_3 {
			background-color: rgba(255, 255, 255, 1.000000);
			width: 710rpx;
			height: 100%;
			display: flex;
			flex-direction: column;
			margin: 20rpx 0 0 20rpx;

			.item {
				// padding: 29rpx 30rpx 30rpx;
				padding: 25rpx 30rpx 20rpx 30rpx;

				.atinput {
					// border: #EBECEE 1rpx solid;
					font-size: 24rpx;
					height: 60rpx;
					width: 600rpx;
					padding-left: 20rpx;
				}

				.item-title {
					color: rgb(153, 153, 153);
					font-size: 28rpx;
					font-weight: 500;
					margin-bottom: 20rpx;
				}

				.value {
					color: rgb(51, 51, 51);
					font-size: 28rpx;
					font-weight: 500;
				}

				.time-cell {
					color: rgb(51, 51, 51);
					font-size: 28rpx;
					font-weight: 500;
					justify-content: flex-start;

					.time {
						width: 49%;

						&:last-of-type {
							margin-left: 20rpx;
						}
					}
				}

				.week-list {
					// display: grid;
					// grid-template-columns: repeat(5, 1fr);
					// grid-gap: 20rpx;
					// position: relative;
					flex-direction: row;
					width: 650rpx;
					font-size: 24rpx;
					flex-wrap: wrap;
					display: flex;

					.week-item {
						padding: 15rpx 0;
						background-color: rgb(243, 243, 243);
						border-radius: 5rpx;
						text-align: center;
						color: rgb(51, 51, 51);
						font-size: 24rpx;
						font-weight: 500;
						border: solid 2rpx transparent;
						box-sizing: border-box;
						padding: 6rpx 12rpx;
						margin-top: 20rpx;
					}

					.active {
						color: rgba(255, 98, 0, 1);
						background-color: rgba(255, 65, 5, 0.1);
						// border: solid 2rpx rgb(99, 93, 247);
						position: relative;

						// &:before {
						// 	display: block;
						// 	content: '';
						// 	position: absolute;
						// 	width: 27rpx;
						// 	height: 28rpx;
						// 	right: -2rpx;
						// 	bottom: -2rpx;
						// 	background-image: url("https://jiangsudiantuo2022.oss-cn-hangzhou.aliyuncs.com/img/46b1cc85fcb0fd76095daf823b7d2bb4a3235519.png");
						// 	background-repeat: no-repeat;
						// 	background-size: 100%;
						// }
					}

					.active1 {
						color: rgba(255, 98, 0, 1);
						background-color: rgba(255, 65, 5, 0.1);
						// border: solid 2rpx rgb(99, 93, 247);
						position: relative;
					}

					.active2 {
						color: rgba(255, 98, 0, 1);
						background-color: rgba(255, 65, 5, 0.1);
						// border: solid 2rpx rgb(99, 93, 247);
						position: relative;
					}

					.check {
						color: rgb(51, 51, 51);
						font-size: 24rpx;
						position: absolute;
						bottom: 10rpx;
						right: 0;
					}
				}
			}
		}

		.text_2 {
			width: 93rpx;
			height: 23rpx;
			overflow-wrap: break-word;
			color: rgba(136, 136, 136, 1);
			font-size: 24rpx;
			font-family: PingFang-SC-Medium;
			font-weight: 500;
			text-align: left;
			white-space: nowrap;
			line-height: 34rpx;
			margin: 15rpx 0 0 21rpx;
		}

		.section_2 {
			background-color: rgba(229, 229, 229, 1.000000);
			width: 672rpx;
			height: 1rpx;
			display: flex;
			flex-direction: column;
			margin: 40rpx 0 0 18rpx;
		}

		.text_3 {
			width: 47rpx;
			height: 23rpx;
			overflow-wrap: break-word;
			color: rgba(136, 136, 136, 1);
			font-size: 24rpx;
			font-family: PingFang-SC-Medium;
			font-weight: 500;
			text-align: left;
			white-space: nowrap;
			line-height: 34rpx;
			margin: 42rpx 0 0 19rpx;
		}

		.text_4 {
			width: 82rpx;
			height: 26rpx;
			overflow-wrap: break-word;
			color: rgba(51, 51, 51, 1);
			font-size: 28rpx;
			font-family: PingFang-SC-Medium;
			font-weight: 500;
			text-align: left;
			white-space: nowrap;
			line-height: 34rpx;
			margin: 24rpx 0 0 20rpx;
		}

		.section_3 {
			background-color: rgba(229, 229, 229, 1.000000);
			width: 672rpx;
			height: 1rpx;
			display: flex;
			flex-direction: column;
			margin: 40rpx 0 0 18rpx;
		}

		.text_5 {
			width: 47rpx;
			height: 23rpx;
			overflow-wrap: break-word;
			color: rgba(136, 136, 136, 1);
			font-size: 24rpx;
			font-family: PingFang-SC-Medium;
			font-weight: 500;
			text-align: left;
			white-space: nowrap;
			line-height: 34rpx;
			margin: 39rpx 0 0 19rpx;
		}

		.text_6 {
			width: 82rpx;
			height: 26rpx;
			overflow-wrap: break-word;
			color: rgba(51, 51, 51, 1);
			font-size: 28rpx;
			font-family: PingFang-SC-Medium;
			font-weight: 500;
			text-align: left;
			white-space: nowrap;
			line-height: 34rpx;
			margin: 24rpx 0 0 20rpx;
		}

		.section_4 {
			background-color: rgba(229, 229, 229, 1.000000);
			width: 672rpx;
			height: 1rpx;
			display: flex;
			flex-direction: column;
			margin: 40rpx 0 0 18rpx;
		}

		.text_7 {
			width: 95rpx;
			height: 23rpx;
			overflow-wrap: break-word;
			color: rgba(136, 136, 136, 1);
			font-size: 24rpx;
			font-family: PingFang-SC-Medium;
			font-weight: 500;
			text-align: left;
			white-space: nowrap;
			line-height: 34rpx;
			margin: 39rpx 0 0 19rpx;
		}

		.image_2 {
			width: 151rpx;
			height: 151rpx;
			margin: 25rpx 0 0 18rpx;
		}


		.text_8 {
			width: 95rpx;
			height: 23rpx;
			overflow-wrap: break-word;
			color: rgba(136, 136, 136, 1);
			font-size: 24rpx;
			font-family: PingFang-SC-Medium;
			font-weight: 500;
			text-align: left;
			white-space: nowrap;
			line-height: 34rpx;
			margin: 20rpx 0 0 19rpx;
		}

		.section_6 {
			width: 378rpx;
			height: 62rpx;
			flex-direction: row;
			display: flex;
			justify-content: space-between;
			margin: 26rpx 0 27rpx 21rpx;
		}

		.button_1 {
			background-color: rgba(243, 243, 243, 1.000000);
			height: 62rpx;
			display: flex;
			flex-direction: column;
			width: 174rpx;
		}

		.text_9 {
			width: 48rpx;
			height: 24rpx;
			overflow-wrap: break-word;
			color: rgba(51, 51, 51, 1);
			font-size: 24rpx;
			font-family: PingFang-SC-Medium;
			font-weight: 500;
			text-align: left;
			white-space: nowrap;
			line-height: 34rpx;
			margin: 19rpx 0 0 63rpx;
		}

		.button_2 {
			background-color: rgba(255, 65, 5, 0.100000);
			height: 62rpx;
			display: flex;
			flex-direction: column;
			width: 174rpx;
		}

		.text_10 {
			width: 73rpx;
			height: 24rpx;
			overflow-wrap: break-word;
			color: rgba(255, 98, 0, 1);
			font-size: 24rpx;
			font-family: PingFang-SC-Medium;
			font-weight: 500;
			text-align: left;
			white-space: nowrap;
			line-height: 34rpx;
			margin: 19rpx 0 0 51rpx;
		}

		.button_3 {
			width: 90%;
			height: 80rpx;
			position: fixed;
			bottom: 20rpx;
			background: linear-gradient(0deg, rgba(214, 53, 53, 0.9), rgba(255, 64, 64, 0.93), rgba(250, 142, 142, 0.93));
			border-radius: 80rpx 80rpx;
			margin-left: 5%;
			color: #fff;
			font-size: 28rpx;
			z-index: 999;
		}
	}
</style>